/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package p_tema6_ejercicio1;

import java.io.*;
/**
 *
 * @author Mow
 */
public class Fichero {
    static BufferedReader fichero;
    File f;
    
    public Fichero(String ruta){
        this.f = new File(ruta);
    }
    
    public File getFichero(){
        return this.f;
    }
    
    public boolean existe_fichero(){
        boolean resul=false;
        if(this.f.exists()){
            resul=true;
        }
        return resul;
    }
    
    public void crear_fichero(){
        try{
            fichero = new BufferedReader(new FileReader(this.f));
        }catch(FileNotFoundException e){
            System.err.println("No se ha podido crear el fichero.");
        }
    }
    
    public void leer_fichero(){
        String cadena = "";
        this.crear_fichero();
        try{
            cadena = fichero.readLine();
            while(cadena != null){
                System.out.println(cadena);
                cadena = fichero.readLine();
            }
            fichero.close();
        }catch(IOException e){
            System.err.println("Error lectura del fichero.");
        }
    }
    
    public void escribir_fichero(int num){
        PrintWriter pw = null;
        try{
               pw = new PrintWriter(new BufferedWriter(new FileWriter(this.f))); 
               for(int i=0;i<=10;i++){
                   pw.println(num+" * "+i+" = "+(num*i));
               }
               pw.close();
            }catch(Exception e1){
                System.err.println("Error en la escritura");
            }
    } 
}
